home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-01-28 | 12.8 KB | 535 lines | [TEXT/MPS ] |
- /*
- File: Application.cp
-
- Contains: TApplication implementation.
-
- This module derived from the Apple Shared Library Manager
- sample source code supplied with ASLM 1.1 (note that ASLM
- is NOT required for, or used in, this project).
-
- Developed by:
-
- Paul G Smith (commstalk hq & Full Moon Software, Inc)
-
- you can leave messages at (UK): 0727 844232; (US): 408 253 7199
- BUT I prefer to be contacted by e-mail
- AppleLink: SMITH.PG
- Internet: SMITH.PG@applelink.apple.com
-
- "SimpliFace" Sample code to accompany develop article
- on techniques for embedding scripts in applications.
-
- */
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- #if MACOS
- #ifndef __QUICKDRAW__
- #include <QuickDraw.h>
- #endif
- #endif
-
- #ifndef __FONTS__
- #include <Fonts.h>
- #endif
- #ifndef __EVENTS__
- #include <Events.h>
- #endif
- #ifndef __WINDOWS__
- #include <Windows.h>
- #endif
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
- #ifndef __DIALOGS__
- #include <Dialogs.h>
- #endif
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
- #ifndef __SEGLOAD__
- #include <SegLoad.h>
- #endif
- #ifndef __OSUTILS__
- #include <OSUtils.h>
- #endif
- #ifndef __TRAPS__
- #include <Traps.h>
- #endif
-
- #ifndef __APPLICATION__
- #include <Application.h>
- #endif
- #ifndef __APPLICATIONCOMMON__
- #include "ApplicationCommon.h"
- #endif
-
- #include "TrapAvailable.cp"
-
- // OSEvent is the event number of the suspend/resume and mouse-moved events sent
- // by MultiFinder. Once we determine that an event is an osEvent, we look at the
- // high byte of the message sent to determine which kind it is. To differentiate
- // suspend and resume events we check the resumeMask bit.
- const short kOsEvent = app4Evt; // event used by MultiFinder
- const short kSuspendResumeMessage = 0x01; // high byte of suspend/resume event message
- const short kClipConvertMask = 0x02; // bit of message field clip conversion
- const short kResumeMask = 0x01; // bit of message field for resume vs. suspend
- const short kMouseMovedMessage = 0xFA; // high byte of mouse-moved event message
-
- /*******************************************************************************
- ** PUBLIC Constructor/Destructor
- ********************************************************************************/
-
- TApplication::TApplication()
- {
- fDone = NULL;
- fInBackground = false;
- fMouseRgn = NULL;
- fWhichWindow = NULL;
- fqd = NULL;
- }
-
- TApplication::TApplication(Ptr qdPtr)
- {
- InitApplication(qdPtr);
- }
-
- TApplication::~TApplication(void)
- {
- }
-
- /*******************************************************************************
- ** PRIVATE InitApplication
- ********************************************************************************/
-
- void TApplication::InitApplication(Ptr qdPtr)
- {
- SysEnvRec envRec;
- long stkNeeded, heapSize;
- fqd = (qdRec*)qdPtr;
-
- // initialize Mac Toolbox components
- InitGraf(&fqd->thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL);
- InitCursor();
-
- // ignore the error returned from SysEnvirons; even if an error occurred,
- // the SysEnvirons glue will fill in the SysEnvRec
- (void) SysEnvirons(curSysEnvVers, &envRec);
-
- // Are we running on a 128K ROM machine or better???
- if (envRec.machineType < 0) {
- TApplication::BigBadError(kApplicationErrStrings,eWrongMachine); // if not, alert & quit
- }
-
- // if we need more stack space, get it now
- stkNeeded = StackNeeded();
- if (stkNeeded > StackSpace())
- {
- // new address is heap size + current stack - needed stack
- SetApplLimit((Ptr) ((long) GetApplLimit() - stkNeeded + StackSpace()));
- }
-
- // Check for minimum heap size
- heapSize = (long) GetApplLimit() - (long) ApplicZone();
- if (heapSize < HeapNeeded()) {
- TApplication::BigBadError(kApplicationErrStrings,eSmallSize);
- }
-
- // expand the heap so new code segments load at the top
- MaxApplZone();
-
- // allocate an empty document list
- // fDocList = new TDocumentList;
-
- // initialize our class variables
- fDone = false;
- fInBackground = false;
- fMouseRgn = NULL;
- fWhichWindow = NULL;
- }
-
- /**********************************************************************
- ** PUBLIC EventLoop
- ***********************************************************************/
-
-
- Boolean TApplication::HandleEvent(EventRecord& /*theEvent*/, Boolean& /*pass*/)
- {
- return false;
- }
-
- void TApplication::InEventLoop(void)
- {
- Boolean gotEvent;
- EventRecord tEvt;
- Boolean pass = false;
- WindowPtr oldWindow;
-
- GetPort(&oldWindow);
-
- // always set up fWhichWindow before doing anything
- fWhichWindow = FrontWindow();
- // make sure we always draw into correct window
- if (fWhichWindow)
- SetPort(fWhichWindow);
-
- DoIdle(); // call idle time handler
-
- gotEvent = WaitNextEvent(everyEvent, &tEvt, SleepVal(), fMouseRgn);
- fTheEvent = tEvt;
-
- // make sure we got a real event
- if ( gotEvent && !HandleEvent(tEvt, pass))
- {
- AdjustCursor();
- switch (tEvt.what)
- {
- case mouseDown :
- DoMouseDown();
- break;
- case mouseUp :
- DoMouseUp();
- break;
- case keyDown :
- case autoKey :
- DoKeyDown();
- break;
- case updateEvt :
- DoUpdateEvt();
- break;
- case diskEvt :
- DoDiskEvt();
- break;
- case activateEvt :
- DoActivateEvt();
- break;
- case kOsEvent :
- DoOSEvent();
- break;
- case kHighLevelEvent:
- DoHighLevelEvent();
- break;
- default :
- break;
- } // end switch (fTheEvent.what)
- }
- AdjustCursor();
-
- SetPort(oldWindow);
- }
-
- void TApplication::EventLoop(void)
- {
- SetUp(); // call setup routine
- DoIdle(); // do idle once
-
- while (fDone == false)
- {
- InEventLoop();
- }
- // call cleanup handler
- CleanUp();
- }
-
- /**********************************************************************
- ** PUBLIC StackNeeded/HeapNeeded
- ***********************************************************************/
-
- long TApplication::StackNeeded()
- {
- return 0;
- }
-
- long TApplication::HeapNeeded()
- {
- return 0;
- }
-
- /**********************************************************************
- ** PUBLIC SetUp/Cleanup
- ***********************************************************************/
-
- void TApplication::SetUp()
- {
- // Run before event loop starts
- }
-
- void TApplication::CleanUp()
- {
- // run at end of loop
- }
-
- /**********************************************************************
- ** PUBLIC ExitLoop
- ***********************************************************************/
-
- void TApplication::ExitLoop(void)
- {
- this->fDone = true;
- }
-
- /**********************************************************************
- ** PUBLIC DoIdle
- ***********************************************************************/
-
- void TApplication::DoIdle()
- {
- // idle time handler (blink caret, background tasks)
- }
-
- /**********************************************************************
- ** PUBLIC AdjustMenus
- ***********************************************************************/
-
- void TApplication::AdjustMenus()
- {
- // menu updater routine
- }
-
- /**********************************************************************
- ** PUBLIC DoKeyDown
- ***********************************************************************/
-
- void TApplication::DoKeyDown(void)
- {
- char key;
- long mResult;
-
- key = (char) (fTheEvent.message & charCodeMask);
- if ((fTheEvent.modifiers & cmdKey) && (fTheEvent.what == keyDown))
- {
- // only do command keys if we are not autokeying
- AdjustMenus(); // make sure menus are up to date
- mResult = MenuKey(key);
- if (mResult != 0) // if it wasn't a menu key, pass it through
- {
- DoMenuCommand(HiWord(mResult), LoWord(mResult));
- return;
- }
- }
- }
-
- /**********************************************************************
- ** PUBLIC DoHighLevelEvent
- ***********************************************************************/
- void TApplication::DoHighLevelEvent(void)
- {
- EventRecord tEvt = fTheEvent;
- // we copy event record so that we don't pass reference to object field
-
- AEProcessAppleEvent(&tEvt);
- }
-
-
-
- /**********************************************************************
- ** PUBLIC DoActivateEvt/DoUpdateEvt
- ***********************************************************************/
-
- void TApplication::DoActivateEvt(void)
- {
- // event record contains window ptr
- fWhichWindow = (WindowPtr) fTheEvent.message;
- SetPort(fWhichWindow);
-
- }
-
- void TApplication::DoUpdateEvt(void)
- {
- // event record contains window ptr
- fWhichWindow = (WindowPtr) fTheEvent.message;
-
- SetPort(fWhichWindow);
- }
-
- /**********************************************************************
- ** PUBLIC DoOSEvent
- ***********************************************************************/
-
- void TApplication::DoOSEvent(void)
- {
- Boolean doConvert;
- unsigned char evType;
-
- // is it a multifinder event?
- evType = (unsigned char) (fTheEvent.message >> 24) & 0x00ff;
- switch (evType) { // high byte of message is type of event
- case kMouseMovedMessage :
- DoIdle(); // mouse-moved is also an idle event
- break;
- case kSuspendResumeMessage :
- doConvert = (fTheEvent.message & kClipConvertMask) != 0;
- fInBackground = (fTheEvent.message & kResumeMask) == 0;
- if (fInBackground)
- DoSuspend(doConvert);
- else DoResume(doConvert);
- break;
- }
- }
-
- /**********************************************************************
- ** PUBLIC DoMouseDown
- ***********************************************************************/
-
- void TApplication::DoMouseDown(void)
- {
- long mResult;
- short partCode;
- WindowPtr tWind;
- EventRecord tEvt;
-
- // gotta watch those object field dereferences
- partCode = FindWindow(fTheEvent.where, &tWind);
- fWhichWindow = tWind;
- tEvt = fTheEvent;
- switch (partCode)
- {
- case inSysWindow :
- DoMouseInSysWindow();
- break;
- case inMenuBar :
- AdjustMenus();
- mResult = MenuSelect(tEvt.where);
- if (mResult != 0)
- DoMenuCommand(HiWord(mResult),LoWord(mResult));
- break;
- case inGoAway :
- DoGoAway();
- break;
- case inDrag :
- DoDrag();
- break;
- case inGrow :
- break;
- case inZoomIn :
- case inZoomOut :
- break;
- case inContent :
- // If window is not in front, make it so
- if ( fWhichWindow != FrontWindow() )
- SelectWindow(fWhichWindow);
- break;
- }
- }
-
- /**********************************************************************
- ** PUBLIC DoMouseInSysWindow
- ***********************************************************************/
-
- void TApplication::DoMouseInSysWindow()
- {
- SystemClick(&fTheEvent, fWhichWindow);
- }
-
- /**********************************************************************
- ** PUBLIC DoDrag
- ***********************************************************************/
-
- void TApplication::DoDrag(void)
- {
- DragWindow(fWhichWindow, fTheEvent.where, &fqd->screenBits.bounds);
- }
-
- /**********************************************************************
- ** PUBLIC DoGoAway
- ***********************************************************************/
-
- void TApplication::DoGoAway(void)
- {
- if (TrackGoAway(fWhichWindow, fTheEvent.where)) {
- CloseDeskAcc(((WindowPeek) fWhichWindow)->windowKind);
-
- fWhichWindow = FrontWindow();
- if (fWhichWindow != NULL) {
- SetPort(fWhichWindow);
- }
- }
- }
-
- /**********************************************************************
- ** PUBLIC AdjustCursor
- ***********************************************************************/
-
- void TApplication::AdjustCursor()
- {
- // cursor adjust routine, should setup mouseRgn
- }
-
- /**********************************************************************
- ** PUBLIC DoMenuCommand
- ***********************************************************************/
-
- void TApplication::DoMenuCommand(short, short)
- {
- }
-
- /**********************************************************************
- ** PUBLIC DoSuspend/DoResume
- ***********************************************************************/
-
- void TApplication::DoSuspend(Boolean /*doClipConvert*/)
- {
- }
-
- void TApplication::DoResume(Boolean /*doClipConvert*/)
- {
- }
-
- /**********************************************************************
- ** PUBLIC DoMenuCommand
- ***********************************************************************/
-
- void TApplication::DoMouseUp()
- {
- }
-
- /**********************************************************************
- ** PUBLIC DoDiskEvt
- ***********************************************************************/
-
- void TApplication::DoDiskEvt()
- {
- }
-
- /**********************************************************************
- ** PUBLIC SleepVal
- ***********************************************************************/
-
- unsigned long TApplication::SleepVal()
- {
- return 20; // how long to sleep in WaitNextEvent
- }
-
- /**********************************************************************
- ** TApplication static methods
- ***********************************************************************/
-
- void TApplication::AlertUser(short errResID, short errCode)
- {
- Str255 message;
-
- GetIndString(message, errResID, errCode);
- #if qDebug
- if (message[0] == 0)
- DebugStr((ConstStr255Param)"\pTApplication::AlertUser could not get error string.");
- #endif
- ParamText(message, (ConstStr255Param)"\p", (ConstStr255Param)"\p", (ConstStr255Param)"\p");
- (void) Alert(rUserAlert, NULL);
- }
-
- void TApplication::BigBadError(short errResID, short errCode)
- {
- TApplication::AlertUser(errResID,errCode);
- ExitToShell();
- }
-